package client_tests;

import net.es.lookup.client.QueryClient;
import net.es.lookup.client.RecordManager;
import net.es.lookup.client.RegistrationClient;
import net.es.lookup.client.SimpleLS;
import net.es.lookup.common.exception.LSClientException;
import net.es.lookup.common.exception.ParserException;
import net.es.lookup.records.ErrorRecord;
import net.es.lookup.records.Record;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 12/18/13
 * Time: 11:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class SerialTest {

    protected ResultTransmitter rx;
    protected SimpleLS sls;
    protected SimpleLS regLS;
    protected RegistrationClient rc;
    protected QueryClient qc;
    protected RecordManager rm;
    protected String targetHost;
    protected int port;
    protected int testRounds;
    protected String sourceHost;
    protected String opCodeBase;
    protected String opCode;
    protected String recordID = null;
    protected LinkedList<String> recordIDs;
    protected final String TYPE = "scalability-test";
    protected final String OPCODE = "op-code";

    public SerialTest() {

    }

    protected void registerTestRecord() {

        recordIDs = new LinkedList<String>();
        String timestamp = null;
        long regTime = 0;
        recordID = "0";

        try {

            DateFormat df = new SimpleDateFormat("MM-dd-HH-mm-ss.SSS");
            regLS = new SimpleLS(targetHost, port);
            rc = new RegistrationClient(regLS);

            regTime = System.currentTimeMillis();
            timestamp = df.format(regTime);
            setOpCode(timestamp);

            System.out.println(sourceHost + "-" + timestamp + ": pre-op registration");

            Record rec = new Record(TYPE);
            rec.add(OPCODE, opCode);

            rc.setRecord(rec);
            rec = rc.register();

            if (!(rec instanceof ErrorRecord)) {

                int index = rec.getURI().lastIndexOf("/") + 1;
                recordID = rec.getURI().substring(index);
                recordIDs.add(recordID);
            }

//            System.err.println(testFinish + ": registration finished after " + (testFinish - regTime) / 1000 + " seconds");
        } catch (LSClientException e) {

            System.out.println(sourceHost + "-" + timestamp + ": pre-op registration failed");
        } catch (ParserException e) {

            System.out.println(sourceHost + "-" + timestamp + ": pre-op registration failed");
        } finally {

//            clearRecords();
        }
    }

    public void setOpCode(String timestamp) {

        opCode = opCodeBase + timestamp;
    }

    public void clearRecords() {

        if (!recordIDs.isEmpty()) {

            for (String id : recordIDs) {

                String action = "/lookup/" + TYPE + "/" + id;
                try {
//TODO: remove the new deleteLS and use sls
                    SimpleLS deleteLS = new SimpleLS(targetHost, port, "DELETE");
                    deleteLS.setRelativeUrl(action);
                    deleteLS.connect();
                    deleteLS.send();
                } catch (LSClientException e) {

                    System.out.println("An exception occurred while trying to delete the test record: " + id);
                    System.out.println("Delete record manually using: curl -v -H \"Content-Type: application/json\" -X DELETE " + targetHost + ":" + port + "/lookup/" + TYPE + "/" + id);
                }
            }
        }

        recordIDs.clear();
    }
}
